using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using 高校员工餐饮信息管理系统.Services;

namespace 高校员工餐饮信息管理系统.Controllers
{
    /// <summary>
    /// 订单控制器
    /// 演示依赖注入在业务逻辑中的应用
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize] // 需要身份验证
    public class OrderController : ControllerBase
    {
        private readonly IOrderService _orderService;
        private readonly ILogger<OrderController> _logger;

        public OrderController(IOrderService orderService, ILogger<OrderController> logger)
        {
            _orderService = orderService;
            _logger = logger;
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> CreateOrder([FromBody] CreateOrderRequest request)
        {
            _logger.LogInformation($"创建订单: 用户={request.UserId}, 商户={request.MerchantId}, 金额={request.TotalAmount}");

            var order = await _orderService.CreateOrderAsync(
                request.UserId, 
                request.MerchantId, 
                request.TotalAmount);

            return Ok(new
            {
                Message = "订单创建成功",
                Order = order
            });
        }

        /// <summary>
        /// 查询用户订单
        /// </summary>
        [HttpGet("user/{userId}")]
        public async Task<IActionResult> GetUserOrders(
            int userId,
            [FromQuery] DateTime? startDate,
            [FromQuery] DateTime? endDate)
        {
            var start = startDate ?? DateTime.Now.AddDays(-30);
            var end = endDate ?? DateTime.Now;

            _logger.LogInformation($"查询用户订单: {userId}, {start:yyyy-MM-dd} ~ {end:yyyy-MM-dd}");

            var orders = await _orderService.GetUserOrdersAsync(userId, start, end);

            return Ok(new
            {
                Message = "查询成功",
                Count = orders.Count,
                Orders = orders
            });
        }

        /// <summary>
        /// 获取订单详情
        /// </summary>
        [HttpGet("{orderId}")]
        public async Task<IActionResult> GetOrderById(int orderId)
        {
            _logger.LogInformation($"获取订单详情: {orderId}");

            var order = await _orderService.GetOrderByIdAsync(orderId);

            if (order == null)
            {
                return NotFound(new { Message = "订单不存在" });
            }

            return Ok(new
            {
                Message = "查询成功",
                Order = order
            });
        }

        /// <summary>
        /// 计算补贴金额
        /// </summary>
        [HttpPost("calculate-subsidy")]
        public async Task<IActionResult> CalculateSubsidy([FromBody] CalculateSubsidyRequest request)
        {
            _logger.LogInformation($"计算补贴: 金额={request.TotalAmount}, 用户={request.UserId}");

            var subsidyAmount = await _orderService.CalculateSubsidyAsync(request.TotalAmount, request.UserId);

            return Ok(new
            {
                Message = "计算成功",
                TotalAmount = request.TotalAmount,
                SubsidyAmount = subsidyAmount,
                SelfPayAmount = request.TotalAmount - subsidyAmount
            });
        }
    }

    public class CreateOrderRequest
    {
        public int UserId { get; set; }
        public int MerchantId { get; set; }
        public decimal TotalAmount { get; set; }
    }

    public class CalculateSubsidyRequest
    {
        public decimal TotalAmount { get; set; }
        public int UserId { get; set; }
    }
}

